package team.archai.nexus.boot.web.response;


import team.archai.nexus.boot.constant.HttpEnum;

/**
 * @program: HttpEnum
 * @description: http响应输出工具类
 * @author: lishibin
 * @create: 8:47 PM
 **/
public class ResultUtil {

    public static <T> ResponseModel<T> success(T data) {
        return success(data, HttpEnum.SUCCESS.getMessage());
    }

    public static <T> ResponseModel<T> success(T data, String successMsg) {
        return out(HttpEnum.SUCCESS.getCode(), successMsg, data);
    }

    public static <T> ResponseModel<T> success() {
        return success(null);
    }

    public static <T> ResponseModel<T> error(String errorMsg) {
        return error(HttpEnum.ERROR.getCode(), errorMsg);
    }

    public static <T> ResponseModel<T> error(Integer code, String errorMsg) {
        return out(code, errorMsg, null);
    }

    public static <T> ResponseModel<T> error(Integer code, String errorMsg, T data) {
        return out(code, errorMsg, data);
    }

    public static <T> ResponseModel<T> error() {
        return error(HttpEnum.ERROR.getMessage());
    }

    private static <T> ResponseModel<T> out(Integer code, String msg, T data) {
        ResponseModel<T> result = new ResponseModel<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * @program: ResultUtil
     * @description:
     * @author: lishibin
     * @create: 11:19 AM
     **/
    public static <T> ResponseModel<T> out(HttpEnum http) {
        ResponseModel<T> result = new ResponseModel<>();
        result.setCode(http.getCode());
        result.setMsg(http.getMessage());
        return result;
    }

    public static <T> ResponseModel<T> out(HttpEnum http, Exception e) {
        ResponseModel<T> result = new ResponseModel<>();
        result.setCode(http.getCode());
        result.setMsg(http.getMessage() + ":" + e.getMessage());
        return result;
    }
}
